Here is the blog post in a clean, simple, and copy-paste-friendly format.
Lesson 1: Ruby Basics for React Developers
Welcome to the first lesson of our React to Rails series.
Before you can build APIs, you need to be comfortable with the language. The good news? If you know JavaScript, you already know 80% of the concepts. We just need to adjust the syntax.
Here is your crash course in Ruby, designed specifically for JavaScript developers.
1. Variables: No More const or let
Ruby is dynamically typed, just like JavaScript, but it is much more minimalistic. You don't need keywords to declare variables.
JavaScript:
let age = 25;
const name = "Bilal";
Ruby:
age = 25
name = "Bilal"
Key Difference:
In Ruby, we use snake_case for variable names (e.g., user_name), whereas JavaScript uses camelCase (e.g., userName).
2. Data Types
Ruby data types map very closely to the types you use in React state.
Integer:
age = 30(Same as JS Number)Float:
price = 19.99(Same as JS Number)String:
name = "Bilal"(Same as JS String)Boolean:
is_active = true(Same as JS Boolean)Nil:
data = nil(Equivalent to JSnullorundefined)
3. String Interpolation
This is one of the most common syntax switches you will need to make.
JavaScript (Backticks):
`Hello ${name}`
Ruby (Double Quotes):
"Hello #{name}"
Important Rule: You must use double quotes "" for interpolation to work in Ruby. Single quotes '' are treated as literal text.
4. Methods (Functions)
In React, you write functions constantly. In Ruby, we call them Methods.
JavaScript:
function greet(name) {
return `Hello ${name}`;
}
Ruby:
Key Differences:
def: Used to define the method.Implicit Return: You do not need to type
return. Ruby automatically returns the result of the last line of code.end: Closes the function (no curly braces{}).
5. Conditionals
If/Else logic is nearly identical, but Ruby offers a cleaner syntax that reads like English.
JavaScript:
if (age >= 18) {
console.log("Adult");
} else {
console.log("Minor");
}
Ruby:
if age >= 18
puts "Adult"
else
puts "Minor"
end
Pro Tip: Ruby allows "trailing conditionals" for one-liners. This is very popular in Rails codebases:
puts "Adult" if age >= 18
6. Output to Console
When debugging your Rails API, you will check your terminal logs, not the browser console.
JS:
console.log("Hello")Ruby:
puts "Hello"
(Note: puts stands for "put string" and adds a new line automatically.)
7. Common Mistakes for React Devs
Forgetting
end: Everydefandifblock must be closed with anend.Falsey Values: In JavaScript,
0is false. In Ruby, it0is true. Onlyfalseandnilare treated as false in Ruby.
π§ Mini Exercise
To lock in this knowledge, try writing this simple Ruby script:
Define a variable
nameandage.Write a method called
introducethat accepts these two variables as arguments.The method should print:
"My name is [NAME] and I am [AGE] years old".Call the method.
(Scroll down for the solution)
Solution:
name = "Bilal"
age = 25
def introduce(user_name, user_age)
puts "My name is #{user_name} and I am #{user_age} years old"
end
introduce(name, age)
What’s Next?
In Lesson 2, we will cover Collections (Arrays & Hashes) and the methods you will use every single day in Rails: map, each, and select.
Next Step for you: Reply with "START LESSON 2" to continue learning!
Comments
Post a Comment